package com.yuhanginfo.modules.sys.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.CrudService;
import com.yuhanginfo.modules.sys.dao.AreaDao;
import com.yuhanginfo.modules.sys.dao.GoodsCombDetailDao;
import com.yuhanginfo.modules.sys.dao.GoodsCombStoreDao;
import com.yuhanginfo.modules.sys.dao.GoodsComboDao;
import com.yuhanginfo.modules.sys.dao.StoreDao;
import com.yuhanginfo.modules.sys.entity.Area;
import com.yuhanginfo.modules.sys.entity.GoodsCombDetail;
import com.yuhanginfo.modules.sys.entity.GoodsCombStore;
import com.yuhanginfo.modules.sys.entity.GoodsCombo;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.StoreArea;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.utils.CodeUtil;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 商品套餐Service
 * @author lvls
 * @version 2017-11-15
 */
@Service
@Transactional(readOnly = true)
public class GoodsComboService extends CrudService<GoodsComboDao, GoodsCombo> {
	public static final Logger logger =  LoggerFactory.getLogger(GoodsComboService.class);
	@Autowired
	private GoodsComboDao goodsComboDao;
	
	@Autowired 
	private AreaDao areaDao;
	
	@Autowired 
	private StoreDao storeDao;
	
	@Autowired 
	private GoodsCombStoreDao goodsCombStoreDao;
	
	@Autowired 
	private GoodsCombDetailDao goodsCombDetailDao;
	
	

	
	
	
	public Page<GoodsCombo> findPage(Page<GoodsCombo> page, GoodsCombo goodsCombo) {
		Page<GoodsCombo> findPage = super.findPage(page, goodsCombo);
		List<GoodsCombo> list = findPage.getList();
		for (GoodsCombo goodsCombo2 : list) {
			List<Store> checkStoreList = goodsCombStoreDao.selectStoreByGoodsComboCode(goodsCombo2.getCode());
			StringBuilder sb = new StringBuilder();
			if(checkStoreList!=null){
				for (Store store : checkStoreList) {
					if(store!=null){
						sb.append(store.getCnName());
						sb.append(";");
					}
				}
			}
			goodsCombo2.setUpStoreNames(sb.toString());
		}
		return findPage;
	}
	
	@Transactional(readOnly = false)
	public void save(GoodsCombo goodsCombo) {
		super.save(goodsCombo);
	}
	
	@Transactional(readOnly = false)
	public void delete(GoodsCombo goodsCombo) {
		super.delete(goodsCombo);
	}
	
	public List<GoodsCombo> getAllGoodsCombo(){
		return goodsComboDao.findList(new GoodsCombo());
	}
	
	@Transactional(readOnly = false)
	public void goodsComboDown(GoodsCombo goodsCombo) {
		goodsCombStoreDao.deleteByGoodsCombo(goodsCombo);
		goodsComboDao.goodsComboDown(goodsCombo);
	}
	
	@Transactional(readOnly = false)
	public void goodsComboUp(GoodsCombo goodsCombo, String storeId) {
		String[] stroreArray = storeId.split(",");
		User user = UserUtils.getUser();
		Date date = new Date();
		List<Store> selectByIds = storeDao.selectByIds(stroreArray);
		for (Store store : selectByIds) {
			GoodsCombStore combStore = new GoodsCombStore();
			combStore.setGoodsCombCode(goodsCombo.getCode());
			combStore.setStoreCode(store.getCode());
			combStore.setCreateBy(user);
			combStore.setCreateDate(date);
			combStore.setUpdateBy(user);
			combStore.setUpdateDate(date);
			combStore.setIsHistory(0);
			goodsCombStoreDao.insert(combStore);
		}
		goodsComboDao.goodsComboUp(goodsCombo);
	}
	@Transactional(readOnly = false)
	public Map<String,Object> getGoodsComboStore(GoodsCombo goodsCombo){
		User user = UserUtils.getUser();
		Map<String,Object> resultMap = new HashMap<>();
		List<Area> areaList=areaDao.areaStoreList();
		List<StoreArea> saList=new ArrayList<>();
		List<Store> findStoreBySysUser = storeDao.findStoreBySysUser(user);
		for(Area a:areaList) {
			StoreArea sa=new StoreArea();
			List<Store> disCheckStores = new ArrayList<>();
			List<Store> checkStores = new ArrayList<>();
			List<String> storeIds = new ArrayList<>();
			sa.setId(a.getId());
			sa.setArea(a.getName());
			List<Store> stores = storeDao.findStoreByClassify(a.getId());
			for (Store store : stores) {
				if(findStoreBySysUser.contains(store)){
					checkStores.add(store);
					storeIds.add(store.getId());
				}else{
					disCheckStores.add(store);
				}
			}
			sa.setStoreIds(storeIds);
			sa.setStoreList(checkStores);
			sa.setDisCheckStoreList(disCheckStores);
			saList.add(sa);
		}
		//获取勾选的门店
		List<Store> checkStoreList = goodsCombStoreDao.selectStoreByGoodsComboCode(goodsCombo.getCode());
		resultMap.put("checkStoreList", checkStoreList);
		resultMap.put("allChecks", saList);
		resultMap.put("resultCode", 1);
		return resultMap;
	}
	@Transactional(readOnly = false)
	public void goodsComboChange(GoodsCombo goodsCombo, String storeId) {
		String[] stroreArray = storeId.split(",");
		User user = UserUtils.getUser();
		Date date = new Date();
		//如果没有选择门店，且是上架状态，则做下架处理
		if(stroreArray.length == 1 && "".endsWith(stroreArray[0])){
			if(goodsCombo.getStatus() == 1){
				goodsComboDao.goodsComboDown(goodsCombo);
			}
		}else{
			//设置套餐为历史记录
			goodsComboDao.setHistoryByGoodsComboCode(goodsCombo);
			//创建一个新的套餐数据
			GoodsCombo newGoodsCombo = new GoodsCombo();
			String goodsComboCode = CodeUtil.getGoodsComboCode();
			newGoodsCombo.setCode(goodsComboCode);
			newGoodsCombo.setIsHistory(0);
			newGoodsCombo.setName(goodsCombo.getName());
			newGoodsCombo.setPhoto(goodsCombo.getPhoto());
			newGoodsCombo.setPrice(goodsCombo.getPrice());
			newGoodsCombo.setRemarks(goodsCombo.getRemarks());
			newGoodsCombo.setStatus(1);
			newGoodsCombo.setCreateBy(user);
			newGoodsCombo.setCreateDate(new Date());
			newGoodsCombo.setUpdateBy(user);
			newGoodsCombo.setUpdateDate(new Date());
			goodsComboDao.insert(newGoodsCombo);
			//获取套餐的详情
			List<GoodsCombDetail> selectByGoodsComboId = goodsCombDetailDao.selectByGoodsComboId(goodsCombo.getId());
			//遍历之前的套餐，将每条数据新建一条新数据
			for (GoodsCombDetail goodsCombDetail : selectByGoodsComboId) {
				GoodsCombDetail goodsCombDetails = new GoodsCombDetail();
				goodsCombDetails.setGoodsCode(goodsCombDetail.getGoodsCode());
				goodsCombDetails.setGoodsCombId(newGoodsCombo.getId());
				goodsCombDetails.setGoodsName(goodsCombDetail.getGoodsName());
				goodsCombDetails.setNum(goodsCombDetail.getNum());
				goodsCombDetails.setOldPrice(goodsCombDetail.getOldPrice());
				goodsCombDetails.setPrice(goodsCombDetail.getPrice());
				goodsCombDetails.setVipPrice(goodsCombDetail.getVipPrice());
				goodsCombDetails.setRemarks(goodsCombDetail.getRemarks());
				goodsCombDetails.setCreateBy(user);
				goodsCombDetails.setCreateDate(new Date());
				goodsCombDetails.setUpdateBy(user);
				goodsCombDetails.setUpdateDate(new Date());
				goodsCombDetailDao.insert(goodsCombDetails);
			}
			//设置套餐上架门店数据为历史记录
			goodsCombStoreDao.setHistoryByGoodsComboCode(goodsCombo);
			List<Store> selectByIds = storeDao.selectByIds(stroreArray);
			for (Store store : selectByIds) {
				GoodsCombStore combStore = new GoodsCombStore();
				combStore.setGoodsCombCode(goodsComboCode);
				combStore.setStoreCode(store.getCode());
				combStore.setCreateBy(user);
				combStore.setCreateDate(date);
				combStore.setUpdateBy(user);
				combStore.setUpdateDate(date);
				combStore.setIsHistory(0);
				combStore.setCreateBy(user);
				combStore.setCreateDate(new Date());
				combStore.setUpdateBy(user);
				combStore.setUpdateDate(new Date());
				goodsCombStoreDao.insert(combStore);
			}
		}
	}

	@Transactional(readOnly = false)
	public void setHistoryByGoodsComboCode(GoodsCombo goodsCombo) {
		goodsComboDao.setHistoryByGoodsComboCode(goodsCombo);
	}

	public List<GoodsCombStore> selectByStoreCode(String code) {
		
		return goodsComboDao.selectByStoreCode(code);
	}
	
}